package com.yanqu.road.dao.impl.player.cricket;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.cricket.*;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CricketEquipSkillDaoImpl extends TempDataObjectDao<CricketEquipSkill> {

    public Map<Integer, List<CricketEquipSkill>> getCricketEquipSkillMap(long userId) {
        Map<Integer, List<CricketEquipSkill>> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_cricket_skill where userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                CricketEquipSkill data = parse(rs);
                if(!dataMap.containsKey(data.getCricketId())){
                    dataMap.put(data.getCricketId(),new ArrayList<>());
                }
                dataMap.get(data.getCricketId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    private static String insertDataSql = "INSERT INTO t_u_cricket_skill(userId,cricketId,skillId,lv) VALUES(?,?,?,?)";

    @Override
    protected boolean addData(CricketEquipSkill data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCricketId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSkillId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLv()));
        if (getDb().executeNoneQuery(insertDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateDataSql = "update `t_u_cricket_skill` set `lv`=? where `userId`=? and `cricketId`=? and `skillId`=?";

    @Override
    protected boolean updateData(CricketEquipSkill data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getLv()));

        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCricketId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSkillId()));
        if (getDb().executeNoneQuery(updateDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CricketEquipSkill parseData(ResultSet rs) throws SQLException {
        CricketEquipSkill data = new CricketEquipSkill();
        data.setUserId(rs.getLong("userId"));
        data.setCricketId(rs.getInt("cricketId"));
        data.setSkillId(rs.getInt("skillId"));
        data.setLv(rs.getInt("lv"));

        return data;
    }
}
